﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InWms
{
    public partial class QueryInBillWnd : UserControl
    {
        #region 成员变量
        private BilInBill _currentQueryRecord;
        private PagerIndex _pagerIndex;
        private BilInBill _selectedRecord;
        public BilInBill SelectedRecord { get { return _selectedRecord; } }
        #endregion

        #region 构造函数
        public QueryInBillWnd()
        {
            InitializeComponent();
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.query;
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.exit;

            cmbBillStatus.SelectedIndex =1;
            InitControl();
            Query();
        }
        #endregion

        #region 公共函数
        public void SetQueryState()
        {
            btnConfirm.Visible = true;
            btnCancel.Visible = true;
        }
        #endregion

        #region 私有函数
        private void InitControl()
        {
            ResetDataPager();
            ResetQueryRecord();
            RefreshData();
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void ResetQueryRecord()
        {
            _currentQueryRecord = new BilInBill();
        }

        private void RefreshData()
        {
            //读入部分数据
            _pagerIndex = new PagerIndex(dataPager.PageCurrent, CommonDefine.iPageSize);
            MyDataBind(_currentQueryRecord, _pagerIndex);
        }

        private void MyDataBind(BilInBill queryRecord, PagerIndex pIndex)
        {
            try
            {
                ServiceReferenceZwms.ServiceZwmsClient client
                    = new ServiceReferenceZwms.ServiceZwmsClient();

                IList<BilInBill> queryList = client.QueryInBillList(queryRecord, ref pIndex);
                dataPager.RecordCount = pIndex.RecordCount;
                UpdateDisplayMode(queryList);
                LoadBillDetail(queryList);
            }
            catch
            {
            }
        }

        private void UpdateDisplayMode(IList<BilInBill> recordList)
        {
            foreach (BilInBill record in recordList)
            {
                record.BillStatus = CommonDefine.DictInBillStatus[record.BillStatus];
            }
        }

        private void LoadBillDetail(IList<BilInBill> recordList)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                this.bilInBillBindingSource.DataSource = null;
                this.bilInBillBindingSource.DataSource = recordList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void Query()
        {
            ResetDataPager();
            ResetQueryRecord();
            _currentQueryRecord.Id = tbBillCode.Text;

            if (chbState.Checked)
            {
                if (cmbBillStatus.SelectedIndex != -1)
                {
                    _currentQueryRecord.BillStatus = cmbBillStatus.SelectedIndex.ToString("D1");
                }
            }
            _currentQueryRecord.WarehouseCode = MainForm.CurrentUser.WarehouseCode;                //限定仓库
            RefreshData();
        }

        private void SelectConfirm()
        {
            if (dgv.CurrentCell == null)
            {
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            _selectedRecord = row.DataBoundItem as BilInBill;
            foreach(KeyValuePair<string,string> kvp in CommonDefine.DictInBillStatus)
            {
                if(_selectedRecord.BillStatus == kvp.Value)
                    _selectedRecord.BillStatus = kvp.Key;
            }

            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.OK;
                window.Close();
            }
            else
            {
                return;
            }
        }
        #endregion

        #region 控件函数
        private void chbState_CheckedChanged(object sender, EventArgs e)
        {
            cmbBillStatus.Enabled = chbState.Checked;
        }

        private void cmbBillStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Query();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void dgv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SelectConfirm();
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            MyDataBind(_currentQueryRecord, _pagerIndex);
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            SelectConfirm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.Cancel;
                window.Close();
            }
            else
            {
                return;
            }
        }
        #endregion

      
    }
}
